# -*- coding:UTF-8 -*-
'''
开发环境：PyCharm
创建日期：2025/8/12 16:45
作   者：新启典学苑-樱桃
描   述：
'''
import random

from ranzhiAutoTest.page.loginpage import LoginPage
from ranzhiAutoTest.util.basepage import BasePage, get_driver
from time import sleep


class AddUserPage(BasePage):
    # 进入添加成员页面需要的元素
    dw_houtai = "id=s-menu-superadmin"  # 后台管理
    dw_iframe = "id=iframe-superadmin"  # 从页面F12中复制
    dw_adduser = "link text=添加成员"  # 添加成员

    # 添加成员页面上的元素
    dw_username = "id=account"  # 用户名
    dw_realname = "id=realname"  # 真实姓名
    dw_genderm = "id=genderm"  # 男
    dw_genderf = "id=genderf"  # 女
    dw_dept = "id=dept"  # 部门
    dw_role = "id=role"  # 角色
    dw_pwd1 = "id=password1"  # 密码1
    dw_pwd2 = "id=password2"  # 密码2
    dw_email = "id=email"  # 邮箱
    dw_save = "id=submit"  # 保存
    dw_wodedipan = 'id=s-menu-dashboard'  # 我的地盘

    dw_pass_query = "id=query"  # 成功时，有搜索输入框

    dw_fail_account = "id=accountLabel"
    dw_fail_realname = "id=realnameLabel"
    dw_fail_role = "id=roleLabel"
    dw_fail_pwd1 = "id=password1Label"
    dw_fail_email = "id=emailLabel"

    # 进入添加成员的页面
    def enter_page(self):
        self.switch_default_conent()
        self.click(self.dw_wodedipan)  # 点击我的地盘，确保之前的页面退出
        sleep(1)
        self.click(self.dw_houtai)
        self.switch_iframe(self.dw_iframe)
        self.click(self.dw_adduser)

    def adduser(self, username, realname, gender, dept, role, pwd1, pwd2, email):
        self.send_keys(self.dw_username, username)
        self.send_keys(self.dw_realname, realname)
        if gender == "男":
            self.click(self.dw_genderm)
        elif gender == "女":
            self.click(self.dw_genderf)
        self.select_by_text(self.dw_dept, dept)
        self.select_by_text(self.dw_role, role)
        self.send_keys(self.dw_pwd1, pwd1)
        self.send_keys(self.dw_pwd2, pwd2)
        self.send_keys(self.dw_email, email)
        self.click(self.dw_save)

    def check_pass(self):
        a = self.locate_element(self.dw_pass_query)
        assert a is not None

    def check_fail(self, expect):
        if "用户名" in expect:
            real = self.locate_element(self.dw_fail_account).text  # 获取界面的错误信息
            print(f"实际错误信息：{real}")
            assert real == expect
        elif "真实姓名" in expect:
            real = self.locate_element(self.dw_fail_realname).text
            print(f"实际错误信息：{real}")
            assert real == expect
        elif "角色" in expect:
            real = self.locate_element(self.dw_fail_role).text
            print(f"实际错误信息：{real}")
            assert real == expect
        elif "密码" in expect:
            real = self.locate_element(self.dw_fail_pwd1).text
            print(f"实际错误信息：{real}")
            assert real == expect
        elif "邮箱" in expect:
            real = self.locate_element(self.dw_fail_email).text
            print(f"实际错误信息：{real}")
            assert real == expect
        else:
            assert False

        # 测试代码，用完删除


if __name__ == '__main__':
    from time import sleep

    driver = get_driver()
    lp = LoginPage(driver)
    lp.open("http://192.168.1.29/ranzhi/www")
    lp.login("admin", "123456")

    aup = AddUserPage(driver)
    aup.enter_page()
    name = "tom" + str(random.randint(1000, 1000000))
    aup.adduser(name, name, "男",
                "/", "项目经理", "123456", "123456", f"{name}@qq.com")
    sleep(2)
    lp.logout()
    lp.quit()
